<?php

namespace app\controllers;

use Yii;
use yii\data\ArrayDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use app\models\RbacForm;
/**
 * 权限控制器
 * Class PermissionController
 * @package app\controllers
 */
class PermissionController extends Controller
{
    /* @var \yii\rbac\DbManager*/
    private $_manager;

    public function init()
    {
        $this->_manager = Yii::$app->authManager;
        parent::init();
    }

    /**
     * 行为
     * @return array
     */
    public function behaviors()
    {
        return [
            [
                'class' => 'app\filters\LoginFilter',
            ],
        ];
    }

    /**
     * Lists all User models.
     * @return mixed
     */
    public function actionIndex()
    {
        $dataProvider = new ArrayDataProvider([
            'allModels'=>$this->_manager->getPermissions(),
            'pagination'=>[
                'pageSize'=>5,
            ],
        ]);

        return $this->render('index', [
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Creates a new User model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new RbacForm(['scenario' => RbacForm::SCENARIO_CREATE_PERMISSION]);
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            if($this->_manager->getPermission($model->permission_name)===null){
                $createPost = $this->_manager->createPermission($model->permission_name);
                $createPost->description = $model->description;
                $this->_manager->add($createPost);
            }
            return $this->redirect(['index']);
        }

        return $this->render('create', [
            'model' => $model,
        ]);
    }

    /**
     * Updates an existing User model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $permission = $this->findItem($id);

        $model = new RbacForm(['scenario' => RbacForm::SCENARIO_CREATE_PERMISSION]);

        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $permission->description = $model->description;
            $permission->name = $model->permission_name;
            $this->_manager->update($id,$permission);
            return $this->redirect(['index']);
        }

        if(Yii::$app->request->isGet){
            $model->description = $permission->description;
            $model->permission_name = $permission->name;
        }

        return $this->render('update', [
                'model' => $model,
            ]);

    }

    /**
     * Deletes an existing User model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $permission = $this->findItem($id);
        $this->_manager->remove($permission);
        return $this->redirect(['index']);
    }

    /**
     * @param $id
     * @return null|\yii\rbac\Item|\yii\rbac\Permission
     * @throws NotFoundHttpException
     */
    protected function findItem($id)
    {
        if (($model = $this->_manager->getPermission($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}